import { findSysDictionary } from '@/api/sysDictionary'
import {defineStore} from 'pinia'

export const useDictionaryStore = defineStore('dictionary',()=> {
    const dictionaryMap = ref({})
    const getDictionary = async ()=> {
        if (dictionaryMap.value[type] && dictionaryMap.value[type].length) {
            return dictionaryMap.value[type]
        } else {
            const res = await findSysDictionary({ type })
            if (res.code === 200) {
                const dict = []
                res.data.resysDictionary.sysDictionaryDetails && res.data.resysDictionary.sysDictionaryDetails.forEach(item => {
                    dict.push({
                        label: item.label,
                        value: item.value
                    })
                })
                dictionaryMap.value[res.data.resysDictionary.type] = dict
                return dictionaryMap.value[type]
            }
        }
    }
    return {
        dictionaryMap,
        getDictionary
    }
})